/*
 * @Author: zhangyang
 * @Date: 2021-02-26 13:54:37
 * @LastEditTime: 2022-03-14 11:13:36
 * @Description: 系统相关的请求
 */
import { basicRequest } from '@/modules/3-net';
import { Params, type AddNodeItem, type AdminUser, type EditNodeItem, type Role } from '@/typings/type';
/**
 * 获取节点列表
 */
export const getNodeList = async () => {
  const task = 5;
  const params = { com: Params.com, task };

  return basicRequest(params) as Promise<EditNodeItem[]>;
};

/**
 * 添加节点
 */
export const addNode = async (nodeItem: AddNodeItem, task = 6) => {
  const params = { com: Params.com, task, ...nodeItem };
  params.node_path = encodeURI(params.node_path);

  return basicRequest(params);
};

/**
 * 编辑节点
 */
export const editNode = async (nodeItem: EditNodeItem) => {
  return addNode(nodeItem);
};

/**
 * 删除节点
 */
export const delNode = async (node_id: number) => {
  const task = 7;
  const params = { com: Params.com, task, node_id };

  return basicRequest(params);
};

/**
 * 获取角色列表
 */
export const getRoleList = async (): Promise<Role[]> => {
  const task = 8;
  const params = { com: Params.com, task };

  return basicRequest(params);
};

/**
 * 添加角色
 */
export const addRole = async (role: Role, task = 9) => {
  const params = { com: Params.com, task, ...role };
  return basicRequest(params);
};

/**
 * 编辑角色
 */
export const editRole = async (role: Role) => {
  return addRole(role);
};

/**
 * 删除角色
 */
export const delRole = async (role_id: number) => {
  const task = 10;
  const params = { com: Params.com, task, role_id };
  return basicRequest(params);
};

/**
 * 获取启用状态的角色列表
 */
const getEnabledRole = async (): Promise<Role[]> => {
  const task = 13;
  const params = { com: Params.com, task };

  return basicRequest(params);
};

/**
 * 获取管理员列表
 */
 export const getAdminList = async (args: any) => {
  const task = 12;
  const params = { com: Params.com, task, ...args };
  const role_list = await getEnabledRole();
  const res = await basicRequest(params);
  const { list = [], total = 0 } = res;
  return { list, total, role_list };
};

/**
 * 添加管理员
 */
export const addAdminUserInfo = async (info: AdminUser, task = 14) => {
  const params = { com: Params.com, task, ...info };
  return basicRequest(params);
};

/**
 * 编辑管理员
 */
export const editAdminUserInfo = addAdminUserInfo;